<?php
/**
 * @file
 * sample_push.php
 *
 * Push demo
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://code.google.com/p/apns-php/wiki/License
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to aldo.armiento@gmail.com so we can send you a copy immediately.
 *
 * @author (C) 2010 Aldo Armiento (aldo.armiento@gmail.com)
 * @version $Id$
 */

// Adjust to your timezone
date_default_timezone_set('America/Toronto');

// Report all PHP errors
error_reporting(-1);

// Using Autoload all classes are loaded on-demand
require_once 'ApnsPHP/Autoload.php';
$messageSent = false;
$feedback = '';
if (empty($_POST) == false) {
	
	if (empty($_POST['apnsAction']) == false) {
		// Instanciate a new ApnsPHP_Push object
		$push = new ApnsPHP_Push(
			ApnsPHP_Abstract::ENVIRONMENT_SANDBOX,
			'apns-dev.pem'
		);
		
		$push->getLogger()->enabled = true;
		// Set the Root Certificate Autority to verify the Apple remote peer
		$push->setRootCertificationAuthority('entrust_root_certification_authority.pem');
		
		// Connect to the Apple Push Notification Service
		$push->connect();
		
		// Instantiate a new Message with a single recipient
		
		//ipod touch device id
		//$message = new ApnsPHP_Message('feef79948417bd411480789e75369360ddf84cdf4e0607763cdb3b1a9dc47129');
		
		//ipad device id
		//$message = new ApnsPHP_Message('5ae5dca77116576c87f7c9fa0f55c4addf433bca04afe5d20df7ed77277e568c');
		
		$message = new ApnsPHP_Message($_POST['deviceId']);
		
		// Set a custom identifier. To get back this identifier use the getCustomIdentifier() method
		// over a ApnsPHP_Message object retrieved with the getErrors() message.
		$message->setCustomIdentifier("Message-Badge-3");
		
		// Set badge icon to "3"
		$message->setBadge(0);
		
		// Set a simple welcome text
		$message->setText($_POST['messageText']);
		
		// Play the default sound
		if ( empty($_POST['soundFile']) == false) {
			$message->setSound($_POST['soundFile']);
		} else {
			$message->setSound();
		}
		
		// Set a custom property
		$message->setCustomProperty('acme2', array('bang', 'whiz'));
		
		// Set another custom property
		$message->setCustomProperty('acme3', array('bing', 'bong'));
		
		// Set the expiry value to 30 seconds
		$message->setExpiry(30);
		
		// Add the message to the message queue
		$push->add($message);
		
		// Send all messages in the message queue
		$push->send();
		
		// Disconnect from the Apple Push Notification Service
		$push->disconnect();
		
		// Examine the error message container
		$aErrorQueue = $push->getErrors();
		if (!empty($aErrorQueue)) {
			var_dump($aErrorQueue);
		} else {
			$messageSent = true;
		}
	} else if (empty($_POST['apnsFeedback']) == false) {
		
		
		$feedbackService = new ApnsPHP_Feedback(
			ApnsPHP_Abstract::ENVIRONMENT_SANDBOX,
			'apns-dev.pem'
		);		
		
		$feedbackService->getLogger()->enabled = true;
		// Set the Root Certificate Autority to verify the Apple remote peer
		//$feedbackService->setRootCertificationAuthority('entrust_root_certification_authority.pem');
		
		// Connect to the Apple Push Notification Service
		$feedbackService->connect();
		
		$feedback = $feedbackService->receive();

		$feedbackService->disconnect();
	}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>

<title> send a message to an iOS device</title>

</head>

<body>
	
	<form method="POST">
	
		<div>
			<label for='deviceId'>Enter Device Id</label>
			<input type='text' name='deviceId' id='deviceId'/>
		</div>
		
		<div>
			<label for='massageText'>Enter Message Text</label>
			<input type='text' name='messageText' id='messageText'/>
		</div>
		
		<div>
			<label for='soundFile'>Enter Sound File Name</label>
			<select name='soundFile' id='soundFile'>
			
				<option value='cat.caf'>Cat</option>
				<option value='frog.caf'>Frog</option>
				<option value='cow.caf'>Cow</option>
				<option value='pig.caf'>Pig</option>
			</select>

		</div>
		
		<div>
			<input type='submit' value='send message'/>
		</div>	
        
        <input type='hidden' value="message" name='apnsAction'/>
	</form>
    
   	<form method="POST">
	
		<div>
			<input type='submit' value='get apns feedback'/>
		</div>	
        
        <input type='hidden' value="message" name='apnsFeedback'/>
	</form>
	
	<div>
		<?php  
		
		if (empty($_POST) == false) {
			if ($messageSent) {
				echo '<p> message was sent successfully</p>';
				echo '<br/>';			
			}
			
			if (empty($feedback) == false) {
				var_dump($feedback);				
				//echo '<p> '..'</p>';	
			} else {
				echo 'no feedback';
			}
								
		}
		?>
	</div>

</body>

</html>